﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace NSEEOD_downloader.Tasks
{
    public class NSEFOEODTask : DownloadTask
    {
        public const string SETTINGS_PARSELINK = "NSE_FO_EOD";
        private string linkTextForParsing = "";

        public NSEFOEODTask()
        {
            linkTextForParsing = Utils.GetString(Application.UserAppDataRegistry.GetValue(SETTINGS_PARSELINK));
        }

        private static string ConvertNSEEODLineToMetaStockASCIILine(string NSEEODLine)
        {
            string MSEODLine = "";
            try
            {
                //NSE:  0        ,  1   , 2       ,  3      , 4        ,  5 ,  6 ,  7,  8  ,   9     , 10      ,  11      ,   12   ,    13   ,     14  ,
                //NSE: INSTRUMENT,SYMBOL,EXPIRY_DT,STRIKE_PR,OPTION_TYP,OPEN,HIGH,LOW,CLOSE,SETTLE_PR,CONTRACTS,VAL_INLAKH,OPEN_INT,CHG_IN_OI,TIMESTAMP,
                //MS:     0    ,  1   , 2    ,  3   , 4   ,  5    ,  6  ,
                //MS:  <ticker>,<date>,<open>,<high>,<low>,<close>,<volume>,<o/i>,
                string[] Values = NSEEODLine.Split(',');
                if (Values[0] != "FUTSTK")
                {
                    return "";
                }
                if (Values[5] == "0")
                {
                    return "";
                }
                if (Values[4] != "XX")
                {
                    return "";
                }

                StringBuilder MSLine = new StringBuilder();
                MSLine.Append(Values[1]); MSLine.Append(",");
                DateTime date = DateTime.Parse(Values[14]);
                MSLine.Append(date.ToString("yyyyMMdd")); MSLine.Append(",");
                MSLine.Append(Values[5]); MSLine.Append(",");
                MSLine.Append(Values[6]); MSLine.Append(",");
                MSLine.Append(Values[7]); MSLine.Append(",");
                MSLine.Append(Values[8]); MSLine.Append(",");
                MSLine.Append(Values[10]); MSLine.Append(",");
                MSLine.Append(Values[12]); MSLine.Append(",");
                MSEODLine = MSLine.ToString();
            }
            catch (Exception err)
            {
                Debug.WriteLine(err.Message);
                return "";
            }
            return MSEODLine;
        }

        protected override string ConvertDownloadedDataToCurrentTaskData(string NSEData)
        {
            StringBuilder Result = new StringBuilder();
            string[] Lines = NSEData.Split('\n');
            Result.AppendLine(Constants.META_STOCK_FO_ASCII_HEADER);
            
            for (int line = 1; line < Lines.Length; line++)
            {
                if (Lines[line].Length < 4)
                {
                    continue;
                }
                string Converted = ConvertNSEEODLineToMetaStockASCIILine(Lines[line]);
                if (Converted.Length < 4)
                {
                    continue;
                }
                Result.AppendLine(Converted);                
            }
            return Result.ToString();
        }

        protected override string ParseLink(string link, DateTime date)
        {
            return Utils.GetLinkFromFormatString(link, date);
        }

        protected override string GetLinkText()
        {
            return linkTextForParsing;
        }
    }
}
